package com.zte.sys.user.control;

import com.zte.sys.dept.model.Dept;
import com.zte.sys.position.model.Job;
import com.zte.sys.user.model.Users;
import com.zte.sys.user.model.UsersCondition;
import com.zte.sys.user.service.IUserService;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserAction {

    @Autowired
    private IUserService userService;
//    @Autowired
//    private RedisTemplate redisTemplate;

     //分页查询
    @RequestMapping("/users/pageUsers")
    @ResponseBody
    @OperatorLog(operate="用户分页查询", module="用户管理")
    public ResultMessage pageUsers(UsersCondition conn, PageModel<Users> pageModel) {
        PageModel<Users> users = userService.queryUserPageByCondition(conn, pageModel);
        ResultMessage rm = new ResultMessage();
        if (users != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(users);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    //详情
    @RequestMapping("sys/users/userView")
    public String userView(){
        return "sys/users/userView";
    }
    @RequestMapping("/users/userDetail")
    @ResponseBody
    @OperatorLog(operate="用户详情", module="用户管理")
    public ResultMessage queryUserByCondition(UsersCondition conn) throws Exception{
        Users users = null;
        if(conn.getUname() != null && !conn.getUname().equals("")){
            users = userService.queryUserByCondition(conn);
        }
        ResultMessage rm = new ResultMessage();
        if (users != null) {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("用户已存在！");
            rm.setData(users);
        } else if(conn.getUname() == null){
            rm.setFlag(false);
            rm.setStatus(401);
            rm.setMsg("请输入用户！");
        }else if(conn.getUname() != null && !conn.getUname().equals("")){
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("成功");
        }
        return rm;
    }

    //修改用户数据回显
    @RequestMapping("/users/userDetail2")
    @ResponseBody
    public ResultMessage queryUserByCondition2(UsersCondition conn) throws Exception{
        Users users = userService.queryUserByCondition(conn);
        ResultMessage rm = new ResultMessage();
        if(users != null){
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("用户已存在！");
            rm.setData(users);
        }else {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("成功");
        }
        return rm;
    }


    //添加
    @RequestMapping("sys/users/userAdd")
    public String userAdd(){
        return "sys/users/userAdd";
    }
    @RequestMapping("/users/userAdd")
    @ResponseBody
//    @OperatorLog(operate="添加用户", module="用户管理")
    public ResultMessage addUsers(Users users){
        System.out.println(users);
        int i = userService.addUser(users);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("添加成功！");
            rm.setData(users);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("添加失败！");
        }
        System.out.println(rm);
        return rm;
    }

    //修改
    @RequestMapping("sys/users/userUpdate")
    public String userUpdate(){
        return "sys/users/userUpdate";
    }
    @RequestMapping("/users/userUpdate")
    @ResponseBody
    @OperatorLog(operate="修改用户", module="用户管理")
    public ResultMessage updateUser(Users users){
        int i = userService.updateUser(users);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(users);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }

    //修改状态
    @RequestMapping("/users/updateStatusId")
    @ResponseBody
    @OperatorLog(operate="修改用户状态", module="用户管理")
    public ResultMessage updateStatusId(Users users){
        int i = userService.updateStatusId(users);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(users);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }

    //查询所有部门
    @RequestMapping("/dept/deptList")
    @ResponseBody
    public ResultMessage deptList(String deptId) {
        List<Dept> depts = userService.queryDeptList(deptId);
        ResultMessage rm = new ResultMessage();
        if (depts != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(depts);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    //查询所有职位
    @RequestMapping("/job/jobList")
    @ResponseBody
    public ResultMessage jobList(String jobId) {
        List<Job> jobs = userService.queryJobList(jobId);
        ResultMessage rm = new ResultMessage();
        if (jobs != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(jobs);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        System.out.println(rm);
        return rm;
    }
}
